package com.zhuang.controller;

import com.github.pagehelper.PageInfo;
import com.zhuang.dto.NotificationDTO;
import com.zhuang.dto.QuestionDTO;
import com.zhuang.mapper.UserMapper;
import com.zhuang.pojo.User;
import com.zhuang.service.NotificationService;
import com.zhuang.service.QuestionService;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ProfileController {

    @Resource
    UserMapper userMapper;

    @Resource
    QuestionService questionService;

    @Autowired
    NotificationService notificationService;

    @GetMapping("/profile/{action}")
    public String profile(@PathVariable(name = "action") String action,
                          Model model,
                          HttpServletRequest request,
                          HttpSession session,
                          @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                          @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {

        User user = (User) session.getAttribute("user");

        Integer count = notificationService.count(user.getAccountId().longValue());

        model.addAttribute("count", count);
        if ("questions".equals(action)) {
            PageInfo<QuestionDTO> result = questionService.list(user.getAccountId(), currentPage, pageSize);
            int firstIndex = 1;
            int lastIndex = 1;
            if (result.getPages() <= 5) {
                lastIndex = result.getPages();
            } else if(result.getPages() >= 5 && currentPage <= 3) {
                lastIndex = 5;
            } else if(result.getPages() >= 5 && currentPage > 3 && currentPage < result.getPages()-2) {
                firstIndex = currentPage - 2;
                lastIndex = currentPage + 2;
            } else {
                firstIndex = result.getPages() - 4;
                lastIndex = result.getPages();
            }
            model.addAttribute("firstIndex", firstIndex);
            model.addAttribute("lastIndex", lastIndex);
            model.addAttribute("result", result);
            model.addAttribute("section", "questions");
            model.addAttribute("sectionName", "我的问题");
        } else if ("replies".equals(action)){
            int firstIndex = 1;
            int lastIndex = 1;
            PageInfo<NotificationDTO> list = notificationService.list(user.getAccountId().longValue(), currentPage, pageSize);
            if (list.getPages() <= 5) {
                lastIndex = list.getPages();
            } else if(list.getPages() >= 5 && currentPage <= 3) {
                lastIndex = 5;
            } else if(list.getPages() >= 5 && currentPage > 3 && currentPage < list.getPages()-2) {
                firstIndex = currentPage - 2;
                lastIndex = currentPage + 2;
            } else {
                firstIndex = list.getPages() - 4;
                lastIndex = list.getPages();
            }

            model.addAttribute("firstIndex", firstIndex);
            model.addAttribute("lastIndex", lastIndex);
            model.addAttribute("nofifyInfo", list);
            model.addAttribute("section", "replies");
            model.addAttribute("sectionName", "我的回复");
        }

        return "profile";
    }
}
